﻿using System.Collections.Generic;
using System.Data;
using Model.EasyUI;
using Utils.Types;
using System;
using SqlHelper;

namespace Service.Permission
{
    public class UserActionService : Repository<Model.Permission.UserAction>
    {
        /// <summary>
        /// 获取操作相关DataTable
        /// </summary>
        /// <param name="ActionType">菜单类型</param>
        /// <param name="UserID">用户ID</param>
        /// <param name="ParentID">父级ID</param>
        /// <returns></returns>
        public DataTable GetUserActionDataTable(int UserID, int ParentID, int ActionType)
        {
            string sql = $"SELECT UserAction.UserActionID,UserAction.ActionName,UserAction.HasPermission,UserAction.ActionID FROM TUserAction AS UserAction INNER JOIN TAction AS actions ON UserAction.ActionID = actions.ActionID where UserAction.UserID={UserID} and UserAction.ParentID={ParentID} and actions.ActionType={ActionType} AND UserAction.HasPermission=1 ";
            return GetDataTableSQL(sql);
        }

        /// <summary>
        /// 管理操作相关DataTable
        /// </summary>
        /// <param name="ActionType">菜单类型</param>
        /// <param name="UserID">用户ID</param>
        /// <param name="ParentID">父级ID</param>
        /// <returns></returns>
        public DataTable ManagerUserActionDataTable(int UserID, int ParentID, int ActionType)
        {
            string sql = $@"SELECT
	                        ActionID,
	                        ActionName,
	                        0 AS HasPermission,
	                        0 AS UserActionID
                        FROM
	                        TAction
                        WHERE
	                        ActionID NOT IN (
		                        SELECT
			                        ActionID
		                        FROM
			                        TUserAction
                                where UserID ={UserID}
	                        )
                        AND ParentID = {ParentID}
                        AND ActionType = {ActionType}
                        UNION
	                       	SELECT
		                    ActionID,
		                    ActionName,
		                    HasPermission,
		                    UserActionID
	                    FROM
		                    TUserAction
	                    WHERE
		                    UserID ={UserID}
	                    AND ActionID IN (
		                    SELECT
			                    ActionID
		                    FROM
			                    TAction
		                    WHERE 
                            ParentID = {ParentID}
                            AND ActionType = {ActionType} ) ";
            return GetDataTableSQL(sql);
        }

        /// <summary>
        /// 获取树形菜单 
        /// </summary>
        /// <param name="ParentID"></param> 
        public List<TreeBox> GetTreeBoxList(int UserID, int ParentID = 0, int ActionType = 0)
        {
            List<TreeBox> list = new List<TreeBox>();
            var actiondt = GetUserActionDataTable(UserID, ParentID, ActionType);
            if (actiondt != null && actiondt.Rows.Count > 0)
            {
                foreach (DataRow item in actiondt.Rows)
                {
                    TreeBox tree = new TreeBox()
                    {
                        id = (int)item["ActionID"],
                        text = item["ActionName"].ToString(),
                        @checked = item["HasPermission"].ConvertInt() > 0,
                        children = GetTreeBoxList(UserID, (int)item["ActionID"], ActionType)
                    };
                    list.Add(tree);
                }
            }
            else
                list = null;
            return list;
        }

        /// <summary>
        /// 管理树形菜单 
        /// </summary>
        /// <param name="ParentID"></param> 
        public List<TreeBox> ManagerTree(int UserID, int ParentID = 0, int ActionType = 0)
        {
            List<TreeBox> list = new List<TreeBox>();
            var actiondt = ManagerUserActionDataTable(UserID, ParentID, ActionType);
            if (actiondt != null && actiondt.Rows.Count > 0)
            {
                foreach (DataRow item in actiondt.Rows)
                {
                    TreeBox tree = new TreeBox()
                    {
                        id = (int)item["ActionID"],
                        text = item["ActionName"].ToString(),
                        @checked = item["HasPermission"].ConvertInt() > 0,
                        children = ManagerTree(UserID, (int)item["ActionID"], ActionType)
                    };
                    list.Add(tree);
                }
            }
            else
                list = null;
            return list;
        }

        /// <summary>
        /// 管理树形操作
        /// </summary>
        /// <param name="list"></param>
        /// <param name="UserID"></param>
        public bool UpdateTree(List<Model.Permission.UserAction> list, int UserID)
        {
            var oldlist = GetListByValue<Model.Permission.UserAction>(UserID, "UserID");
            if (oldlist != null && oldlist.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                    for (int j = 0; j < oldlist.Count; j++)
                        if (list[i].ActionID == oldlist[j].ActionID)
                        {
                            oldlist[j].HasPermission = list[i].HasPermission;
                            oldlist[j].UserID = UserID;
                        }
                return UpdateList(oldlist);
            }
            else
            {
                var templist = GetAllList<Model.Permission.Action>();
                if (templist != null && templist.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                        for (int j = 0; j < templist.Count; j++)
                            if (list[i].ActionID == templist[j].ActionID)
                            {
                                list[i].UserID = UserID;
                                list[i].CreateTime = list[i].UpdateTime = DateTime.Now;
                                list[i].ActionName = templist[j].ActionName;
                                list[i].ParentID = templist[j].ParentID;
                            }
                    return InsertList(list);
                }
                return false;
            }
        }


    }
}
